package org.example.tomcat;


import java.io.IOException;
import java.io.OutputStream;

import java.util.HashMap;
import java.util.Map;


//响应类 负责写http响应 主要包括响应行响应头响应体
public class MyResponse extends AbstractHttpServletResponse{
    private byte SP=' ';
    private byte CR='\r';
    private byte LF='\n';

    private int status=200;
    private String message="OK";
    private  Map<String, String> headers = new HashMap<>();
    private String contentType = "text/html";

    private OutputStream socketOutputStream;
    private MyRequest request;


    private ResponseServletOutputStream outputStream=new ResponseServletOutputStream();

    public MyResponse( MyRequest request)  {
        try {
            this.socketOutputStream = request.getSocket().getOutputStream();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        this.request = request;
    }

    // 写响应
    public void complete(){
        sendResponseLine();
        sendResponseHeaders();
        sendResponseBody();

    }

    private void sendResponseBody() {
        try {
            ResponseServletOutputStream outputStream =  getOutputStream();
            socketOutputStream.write(outputStream.getBytes());

        }
        catch (IOException e){
            e.printStackTrace();
        }
    }

    private void sendResponseHeaders() {
        if(!headers.containsKey("content-Length")){
            headers.put("Content-Length", String.valueOf(outputStream.getIndex()));
        }

        for (Map.Entry<String, String> stringStringEntry : headers.entrySet()) {
            try {
                socketOutputStream.write(stringStringEntry.getKey().getBytes());
                socketOutputStream.write(":".getBytes());
                socketOutputStream.write(stringStringEntry.getValue().getBytes());
                socketOutputStream.write(CR);
                socketOutputStream.write(LF);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        try {
            socketOutputStream.write(CR);
            socketOutputStream.write(LF);
        } catch (IOException e) {
            e.printStackTrace();
        }
        
    }


    //发送响应行 按规范写即可
    private void sendResponseLine()   {

        try {
            socketOutputStream.write(request.getProtocol().getBytes());
            socketOutputStream.write(SP);
            socketOutputStream.write(String.valueOf(status).getBytes());
            socketOutputStream.write(SP);
            socketOutputStream.write(message.getBytes());
            socketOutputStream.write(CR);
            socketOutputStream.write(LF);
        }
        catch (IOException e){
            e.printStackTrace();
        }



    }


    @Override
    public ResponseServletOutputStream getOutputStream() throws IOException {
        return  outputStream;
    }

    @Override
    public void setStatus(int status) {
        this.status = status;
    }

    public void setHeaders(Map<String, String> headers) {
        this.headers = headers;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    @Override
    public int getStatus() {
        return status;
    }

    public String getMessage() {
        return message;
    }

    public Map<String, String> getHeaders() {
        return headers;
    }

    @Override
    public void addIntHeader(String s, int i) {
        headers.put(s, String.valueOf(i));
    }


    @Override
    public String getContentType() {
        return contentType;
    }

    @Override
    public void setContentType(String contentType) {
        this.contentType = contentType;
    }


}
